/*
 * Created by Wang Lei on 21-07-01.
 * Copyright (c) 2006-2021 Kaitiren
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace HLLib
{
    /// <summary>
    /// HLAnimationEventListener
    /// </summary>
    public class HLAnimationEventListener : MonoBehaviour
    {
        /// <summary>
        /// ��ǰHLGameObject.
        /// hlGameObject
        /// </summary>
        public HLGameObject hlGameObject;

        /// <summary>
        /// ��ǰ����Ƭ���¼�����.
        /// animationcliplist
        /// </summary>
        public HLAnimationClipList hlAnimationClipList;

        /// <summary>
        /// ������Ⱦ���� hlWillRenderGameObject.
        /// </summary>
        public HLWillRenderGameObject hlWillRenderGameObject;
        

        /// <summary>
        /// Awake
        /// </summary>
        void Awake()
        {
            foreach (Transform _trans in this.gameObject.transform)
            {
                Renderer _script_renderer = _trans.gameObject.GetComponent<Renderer>() as Renderer;
                if (_script_renderer != null)
                    this.hlWillRenderGameObject 
                        = _trans.gameObject.AddComponent<HLWillRenderGameObject>();
            }
        }


        /// <summary>
        /// Start is called before the first frame update.
        /// </summary>
        void Start()
        {
            hlGameObject = null;
            hlAnimationClipList = null;
            hlWillRenderGameObject = null;
        }


        /// <summary>
        /// ClearHLAnimationEventListener
        /// </summary>
        public void ClearHLAnimationEventListener()
        {
            if (null != hlAnimationClipList)
            {

            }
            this.hlGameObject = null;
        }


        /// <summary>
        /// ���HLAnimationEventListener����AnimationClipList.
        /// AddHLAnimationEventListenerReturnAnimationClipList
        /// </summary>
        /// <param name="_hlGameObject">hlGameObject</param>
        public HLAnimationClipList AddHLAnimationEventListenerReturnHLAnimationClipList(HLGameObject _hlGameObject)
        {
            this.hlGameObject = _hlGameObject;
            
            // get tranform:
            this.hlGameObject.transform = this.gameObject.transform;

            // get animator:
            hlGameObject.m_animator = this.gameObject.GetComponent<Animator>();

            // get gameObject Children Renderers.
            if (hlGameObject.childrenRenders == null)
            {
                // get gameobject children renderers.
                hlGameObject.childrenRenders = hlGameObject.gameobject.GetComponentsInChildren<Renderer>(true);
            }

            // get ridbody.
            hlGameObject.rigdbody = this.gameObject.GetComponent<Rigidbody>();

            // get boxcollider array:
            hlGameObject.boxcolliderArray = this.gameObject.GetComponents<BoxCollider>();

            return hlAnimationClipList;
        }


        #region ------------------------ Trigger -----------------------------
        /// <summary>
        /// 
        /// OnTriggerEnter
        /// </summary>
        /// <param name="collider"></param>
        void OnTriggerEnter(Collider _collider)
        {
            if (null == hlGameObject
                || hlGameObject.rigdbody)
                return;
            Debug.Log("collider.name: " + _collider.name);
            //Debug.Log("On Enter Trigger Att: " + this.gameObject.name + " ,Touch: " + collider.gameObject.name);
            HLGameObject _touch_hlgameobj = _collider.gameObject.GetComponent<HLAnimationEventListener>().hlGameObject;
            HLGameObject[] hlGameObjectArray = new HLGameObject[2];
            hlGameObjectArray[0] = hlGameObject;
            hlGameObjectArray[1] = _touch_hlgameobj;
            // callback: 
            //GameScreen.SharedGameScreenManager()._GlobalObj.SendMessage("physicalImpactGameObject", hlGameObjectArray);
        }


        /// <summary>
        /// �����˳�.
        /// OnTriggerExit
        /// </summary>
        /// <param name="_collider"></param>
        void OnTriggerExit(Collider _collider)
        {

        }


        /// <summary>
        /// ��������.
        /// OnTriggerStay
        /// </summary>
        /// <param name="_collider"></param>
        void OnTriggerStay(Collider _collider)
        {

        }

        #endregion


        #region ------------------------ Collision -------------------------------
        /// <summary>
        /// ��ײ��������.
        /// OnCollisionEnter
        /// </summary>
        /// <param name="collisionInfo"></param>
        void OnCollisionEnter(Collision _collisionInfo)
        {
            if (null == hlGameObject
                || null == hlGameObject.rigdbody)
                return;
            Debug.Log("On Hit:" + _collisionInfo.gameObject.name + "!!!");
        }


        /// <summary>
        /// ��ײ�����˳�.
        /// OnCollisionExit
        /// </summary>
        /// <param name="_collisionInfo"></param>
        void OnCollisionExit(Collision _collisionInfo)
        {
            if (null == hlGameObject
                || null == hlGameObject.rigdbody)
                return;

        }


        /// <summary>
        /// ��ײ��������.
        /// OnCollisionStay
        /// </summary>
        /// <param name="collisionInfo"></param>
        void OnCollisionStay(Collision collisionInfo)
        {
            if (null == hlGameObject
                || null == hlGameObject.rigdbody)
                return;

        }


        /// <summary>
        /// ��ɫ����������������Collider�������֮�����ײ
        /// ControllerColliderHit
        /// </summary>
        /// <param name="hit"></param>
        void OnControllerColliderHit(ControllerColliderHit hit)
        {
            if (null == hlGameObject
                || null == hlGameObject.rigdbody)
                return;

        }

        #endregion


    }
}

